package cn.yhbk.web.modules.manager.Controller;

import cn.hutool.core.bean.BeanUtil;
import cn.yhbk.web.common.api.CommonResult;
import cn.yhbk.web.modules.manager.Pojo.Entity.OrderResult;
import cn.yhbk.web.modules.manager.Pojo.Entity.OrderResultItem;
import cn.yhbk.web.modules.manager.Pojo.VO.OrderResult.*;
import cn.yhbk.web.modules.manager.Service.IOrderResultItemService;
import cn.yhbk.web.modules.manager.Service.IOrderResultService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@Api(tags = "排行自定义集合管理接口")
@RequestMapping("/manager/orderResult")
public class OrderResultController {

    @Autowired
    IOrderResultService orderResultService;

    @Autowired
    IOrderResultItemService itemService;

    @ApiOperation("添加自定义集合")
    @PostMapping("/")
    public CommonResult addOrderResult(@Valid @RequestBody AddOrderResult addOrderResult){
        OrderResult orderResult = new OrderResult();
        BeanUtil.copyProperties(addOrderResult, orderResult);

        boolean success = orderResultService.save(orderResult);
        if(success)return CommonResult.success("添加成功!");

        return CommonResult.failed("添加失败!");
    }

    @ApiOperation("获取自定义集合")
    @GetMapping("/")
    public CommonResult getOrderResult(@RequestParam(value = "currentPage",defaultValue = "1")Long currentPage,
                                       @RequestParam(value = "pageSize",defaultValue = "5")Long pageSize,
                                       OrderResultParam condition){
        Map<String, Object> orderResult = orderResultService.getOrderResult(currentPage, pageSize, condition);
        return CommonResult.success(orderResult);
    }

    @ApiOperation("修改自定义集合信息")
    @PutMapping("/{orderResultId}")
    public CommonResult orderResultModify(@PathVariable Long orderResultId,@RequestBody OrderResultModify modify){
        boolean success = orderResultService.orderResultModify(orderResultId,modify);
        if(success)return CommonResult.success("修改成功!");
        return CommonResult.failed("修改失败!");
    }

    @ApiOperation("删除自定义集合")
    @DeleteMapping("/{orderResultId}")
    public CommonResult orderResultDel(@PathVariable Long orderResultId){

        orderResultService.removeById(orderResultId);
        itemService.removeByResultId(orderResultId);
        return CommonResult.success("删除成功!");
    }

    @ApiOperation("添加集合元素")
    @PostMapping("/item")
    public CommonResult setItem(@RequestBody ItemVO add){
        Long resultId = add.getOrderResultId();
        OrderResult result = orderResultService.getById(resultId);
        if(Objects.isNull(result))return CommonResult.failed("添加失败！该集合不存在");

        OrderResultItem item = new OrderResultItem();
        BeanUtil.copyProperties(add,item);

        boolean success = itemService.save(item);

        if(success)return CommonResult.success("添加成功!");


        return CommonResult.failed("添加失败!");

    }


    @ApiOperation("删除集合元素")
    @DeleteMapping("/item")
    public CommonResult delItem(Long itemId){

        boolean success = itemService.removeById(itemId);

        if(success)return CommonResult.success("删除成功!");

        return CommonResult.failed("删除失败!");

    }

}
